using UnityEngine;
using System.Collections;

public class SceneManager : MonoBehaviour{
		
	/// <summary>
	/// The m finger touch.
	/// </summary>
	private bool mFingerTouch = false;
	
	/// <summary>
	/// The m finger down position.
	/// </summary>
	private Vector3 mFingerDownPos = Vector3.zero;
	
	/// <summary>
	/// The indicator for leading character to move
	/// </summary>
	private PackedSprite	mIndicator = null;
	
	/// <summary>
	/// The state of indicator disappearing.
	/// </summary>
	private bool			mIndicatorDisappearing = false;
	private float			mIndicatorDisTimer = 0;
	
	public GameCha			mTest;
	
	// Use this for initialization
	void Start () {
		
		// first enable finger touch
		SetEnableFingerTouch(true);
	}
	
	// Update is called once per frame
	void Update () {
		UpdateIndicatorAlpha();
	}
					
	
	/// <summary>
	/// Updates the indicator alpha when disappear
	/// </summary>
	private void UpdateIndicatorAlpha(){
		
		if(mIndicatorDisappearing){
			
			const float disappearTime = 0.5f;
			
			mIndicatorDisTimer += Time.deltaTime;
			if(mIndicatorDisTimer > disappearTime){
				mIndicatorDisTimer = disappearTime;
				mIndicatorDisappearing = false;
			}
			
			float delta = mIndicatorDisTimer / (disappearTime);
			mIndicator.SetColor(new Color(mIndicator.Color.r,mIndicator.Color.g,mIndicator.Color.b,1 - delta));
			
			if(!mIndicatorDisappearing){
				mIndicator.gameObject.SetActiveRecursively(false);
				
			}
		}
	}
	
	/// <summary>
	/// Sets the disappear indicator state
	/// </summary>
	/// <param name='disappear'>
	/// Disappear.
	/// </param>
	private void SetDisappearIndicator(bool disappear){
		
		mIndicatorDisappearing = disappear;
		
		if(disappear){
			mIndicatorDisTimer = 0;
		}else{
			mIndicator.gameObject.SetActiveRecursively(true);
			mIndicator.SetColor(Color.white);
		}
		
	}
	
	/// <summary>
	/// Inits the indicator.
	/// </summary>
	private void InitIndicator(){
		if(mIndicator == null){
			GameObject go = GameObject.Instantiate(Resources.Load("Speff/Indicator/Indicator")) as GameObject;
			mIndicator = go.GetComponent<PackedSprite>();
		}
	}

	/// <summary>
	/// Sets the enable finger touch.
	/// </summary>
	/// <param name='enable'>
	/// Enable or disable
	/// </param>
	public void SetEnableFingerTouch(bool enable){
		if(mFingerTouch != enable){
			mFingerTouch = enable;
			
			if(enable){
				
				FingerGestures.OnFingerDown += OnFingerDown;
				FingerGestures.OnFingerUp	+= OnFingerUp;
				FingerGestures.OnFingerMove += OnFingerMove;
				
			}else{
				
				FingerGestures.OnFingerDown -= OnFingerDown;
				FingerGestures.OnFingerUp	-= OnFingerUp;
				FingerGestures.OnFingerMove -= OnFingerMove;
			}
		}
	}
	
	private void OnFingerDown(int fingerIndex, Vector2 fingerPos){
		if(Utils.RayTestGameCha(new Vector3(fingerPos.x,fingerPos.y,0),ref mFingerDownPos)){
			// initialize the indicator
			InitIndicator();			
			mIndicator.gameObject.SetActiveRecursively(true);
			
			mFingerDownPos.y = 0.1f;
			mIndicator.transform.position = mFingerDownPos;
			mIndicator.transform.localScale = Vector3.zero;
			
			SetDisappearIndicator(false);
		}
	}
	
	private void OnFingerUp(int fingerIndex, Vector2 fingerPos, float timeHeldDown) {
		SetDisappearIndicator(true);
		
		Vector3 pos = Vector3.one;
		if(Utils.RayTestTerrain(new Vector3(fingerPos.x,fingerPos.y,0),ref pos)){
			pos.y = 0;
			mTest.MoveTo(pos);
		}
		
	}
	
	private void OnFingerMove(int fingerIndex, Vector2 fingerPos){
		
		// move the indicator
		if(mIndicator.gameObject.active){
			Vector3 pos = Vector3.one;
			if(Utils.RayTestTerrain(new Vector3(fingerPos.x,fingerPos.y,0),ref pos)){
				pos = (pos - mFingerDownPos);
				
				float length = pos.magnitude;
				pos.Normalize();
				
				mIndicator.transform.forward = pos;
				mIndicator.transform.localScale = new Vector3(0.3f,1,length);
			}	
		}
	}
}
